package com.yw.swagger.controller;

import com.yw.swagger.customNote.fyh;
import com.yw.swagger.model.SysBpsUsers;
import com.yw.swagger.model.WebMail;
import com.yw.swagger.model.WebMailUpload;
import com.yw.swagger.service.SysBpsUsersService;
import com.yw.swagger.service.WebMailService;
import com.yw.swagger.tool.AjaxResult;
import com.yw.swagger.tool.Utils.StringUtils;
import com.yw.swagger.tool.controller.BaseController;
import com.yw.swagger.tool.page.TableDataInfo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.*;


@Controller
@Api(tags = "邮件操作")
@RequestMapping("/web_mail")
public class WebMailController extends BaseController {

    @Autowired
    WebMailService webMailService;

    @Autowired
    private SysBpsUsersService sysBpsUsersService;

    @fyh
    @ApiOperation(value = "新增发送邮件", notes = "新增发送邮件")
    @ApiImplicitParam(name = "author", value = "权限码", required = true)
    @ApiResponses({@ApiResponse(code = 200, message = "成功"), @ApiResponse(code = 500, message = "error")})
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(WebMail webMail, WebMailUpload upload) {
        System.err.println("接收人:" + webMail.getToSendName());
        String Name[] = webMail.getToSendName().split(",");
        List<SysBpsUsers> list = sysBpsUsersService.selectUserAccount(Name);
        String name = "";
        String account = "";
        for (int i = 0; i < list.size(); i++) {
            if (account.equals("")) {
                account = account.trim() + list.get(i).getUSER_Account().trim();
            } else {
                account = account.trim() + "," + list.get(i).getUSER_Account().trim();
            }
        }
        String uuid = UUID.randomUUID().toString();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        webMail.setSendUser(webMail.getSendUser() + sdf.format(date));
        webMail.setGuid(uuid);
        webMail.setSendTime(date);
        webMail.setFirstSend("1");
        webMail.setToAllSend(webMail.getToSendName());
        webMail.setToSend(webMail.getToSendName());
        webMail.setToSendAccount(account);
        int i = webMailService.insertWebMail(webMail);
        String[] send = webMail.getToAllSend().split(",");
        String[] sendName = webMail.getToSendAccount().split(",");
        for (int k = 0; k < send.length; k++) {
            for (int j = 0; j < sendName.length; j++) {
                webMail.setToSendName(send[k]);
                webMail.setToSend(sendName[j + k]);
                webMail.setToAllSend(send[k]);
                webMail.setFirstSend(null);
                webMailService.insertWebMail(webMail);
                break;
            }
        }
        System.err.println("附件名字:" + upload.getFilename());
        System.err.println("附件路径:" + upload.getFilepath());
        //保存附件

        webMailService.insertWebMailF(webMail, upload);
        return AjaxResult.success(i);
    }

    @fyh
    @ApiOperation(value = "分页查询所有邮件", notes = "分页查询所有邮件")
    @ApiResponses({@ApiResponse(code = 200, message = "成功"), @ApiResponse(code = 500, message = "error")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "author", value = "权限码", required = true),
            @ApiImplicitParam(name = "title", value = "标题"),
    })
    @PostMapping("/WebMailAll")
    @ResponseBody
    public AjaxResult allList(String title) {
        startPage();
        List<WebMail> l = webMailService.selectWebMailAll(title);
        for (WebMail mail : l) {
            String sendUser = mail.getSendUser();
            if (sendUser.length() > 14) {
                mail.setSendUser(sendUser.substring(0, sendUser.length() - 14));
            }
        }
        TableDataInfo td = getDataTable(l);
        AjaxResult ar = new AjaxResult(AjaxResult.Type.SUCCESS, null);
        ar.put("rows", td.getRows());
        ar.put("total", td.getTotal());
        return ar;
    }


    @fyh
    @ApiOperation(value = "分页查询某发件人全部邮件", notes = "分页查询某发件人全部邮件")
    @ApiResponses({@ApiResponse(code = 200, message = "成功"), @ApiResponse(code = 500, message = "error")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "author", value = "权限码", required = true),
            @ApiImplicitParam(name = "sendUser", value = "发件人", required = true),
            @ApiImplicitParam(name = "title", value = "标题"),
    })
    @PostMapping("/sendUserAll")
    @ResponseBody
    public AjaxResult fromList(String sendUser, String title) {
        WebMail mail = new WebMail();
        mail.setSendUser(sendUser);
        mail.setTitle(title);
        startPage();
        List<WebMail> l = webMailService.selectMailSendUserAll(mail);
        for (WebMail webmail : l) {
            String sendUser1 = webmail.getSendUser();
            if (sendUser1.length() > 14) {
                webmail.setSendUser(sendUser1.substring(0, sendUser1.length() - 14));
            }
        }
        TableDataInfo td = getDataTable(l);
        AjaxResult ar = new AjaxResult(AjaxResult.Type.SUCCESS, null);
        ar.put("rows", td.getRows());
        ar.put("total", td.getTotal());
        return ar;
    }

    @fyh
    @ApiOperation(value = "分页查询某收件人全部邮件", notes = "分页查询某收件人全部邮件")
    @ApiResponses({@ApiResponse(code = 200, message = "成功"), @ApiResponse(code = 500, message = "error")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "author", value = "权限码", required = true),
            @ApiImplicitParam(name = "toAllSend", value = "收件人", required = true),
            @ApiImplicitParam(name = "title", value = "标题"),
    })
    @PostMapping("/toAllSend")
    @ResponseBody
    public AjaxResult toList(String toAllSend, String title) {
        WebMail mail = new WebMail();
        mail.setToAllSend(toAllSend);
        mail.setTitle(title);
        startPage();
        List<WebMail> l = webMailService.selectMailtoAllSend(mail);
        for (WebMail webmail : l) {
            String sendUser1 = webmail.getSendUser();
            if (sendUser1.length() > 14) {
                webmail.setSendUser(sendUser1.substring(0, sendUser1.length() - 14));
            }
        }
        TableDataInfo td = getDataTable(l);
        AjaxResult ar = new AjaxResult(AjaxResult.Type.SUCCESS, null);
        ar.put("rows", td.getRows());
        ar.put("total", td.getTotal());
        return ar;
    }

    @fyh
    @ApiOperation(value = "收邮件人查询单个邮件详情信息", notes = "收邮件人查询单个邮件详情信息")
    @ApiResponses({@ApiResponse(code = 200, message = "成功"), @ApiResponse(code = 500, message = "error")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "author", value = "权限码", required = true),
            @ApiImplicitParam(name = "id", value = "邮件id", required = true),
            @ApiImplicitParam(name = "guid", value = "编码", required = true)
    })
    @PostMapping("/detail")
    @ResponseBody
    public AjaxResult noticeInfo(int id, String guid) {
        //查询WebMail表详细信息
        WebMail webMail = new WebMail();
        webMail.setID(id);
        webMail.setGuid(guid);
        WebMail mail = webMailService.selectWebMail(webMail);
        if (StringUtils.isNull(mail)) {
            return error("没有数据");
        }
        String sendUser1 = mail.getSendUser();
        if (sendUser1.length() > 14) {
            mail.setSendUser(sendUser1.substring(0, sendUser1.length() - 14));
        }
        //标记邮件为已读
        webMailService.updateMailRead(webMail);
        //查询WebMail附表表详细信息
        List<WebMailUpload> mailFile = webMailService.selectNoticeFileByNoticeId(guid);
        Map m = new HashMap();
        m.put("mail", mail);
        m.put("mailFile", mailFile);
        return AjaxResult.success(m);
    }

    @fyh
    @ApiOperation(value = "发邮件人查询单个邮件详情信息", notes = "发邮件人查询单个邮件详情信息")
    @ApiResponses({@ApiResponse(code = 200, message = "成功"), @ApiResponse(code = 500, message = "error")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "author", value = "权限码", required = true),
            @ApiImplicitParam(name = "邮件id", value = "id", required = true),
            @ApiImplicitParam(name = "guid", value = "编码", required = true)
    })
    @PostMapping("/detailSend")
    @ResponseBody
    public AjaxResult noticeInfoSend(int id, String guid) {
        //查询WebMail表详细信息
        WebMail webMail = new WebMail();
        webMail.setID(id);
        WebMail mail = webMailService.selectWebMail(webMail);
        if (StringUtils.isNull(mail)) {
            return error("没有数据");
        }
        String sendUser1 = mail.getSendUser();
        if (sendUser1.length() > 14) {
            mail.setSendUser(sendUser1.substring(0, sendUser1.length() - 14));
        }
        //查询WebMail附表表详细信息
        List<WebMailUpload> mailFile = webMailService.selectNoticeFileByNoticeId(guid);
        Map m = new HashMap();
        m.put("mail", mail);
        m.put("mailFile", mailFile);
        return AjaxResult.success(m);
    }

    @fyh
    @ApiOperation(value = "分页查询收件人全部未读邮件", notes = "分页查询收件人全部未读邮件")
    @ApiResponses({@ApiResponse(code = 200, message = "成功"), @ApiResponse(code = 500, message = "error")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "author", value = "权限码", required = true),
            @ApiImplicitParam(name = "toSend", value = "收件人账号", required = true),
            @ApiImplicitParam(name = "title", value = "标题"),
    })
    @PostMapping("/to_mail_unread")
    @ResponseBody
    public AjaxResult toUnread(String toSend, String title) {
        WebMail mail = new WebMail();
        mail.setToSend(toSend);
        mail.setTitle(title);
        startPage();
        //查看指定账号的全部未读邮件
        List<WebMail> l = webMailService.selectMailUnread(mail);
        for (WebMail webmail : l) {
            String sendUser1 = webmail.getSendUser();
            if (sendUser1.length() > 14) {
                webmail.setSendUser(sendUser1.substring(0, sendUser1.length() - 14));
            }
        }
        TableDataInfo td = getDataTable(l);
        AjaxResult ar = new AjaxResult(AjaxResult.Type.SUCCESS, null);
        ar.put("rows", td.getRows());
        ar.put("total", td.getTotal());
        return ar;
    }

    @fyh
    @ApiOperation(value = "分页查询收件人全部已读邮件", notes = "分页查询收件人全部已读邮件")
    @ApiResponses({@ApiResponse(code = 200, message = "成功"), @ApiResponse(code = 500, message = "error")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "author", value = "权限码", required = true),
            @ApiImplicitParam(name = "toSend", value = "收件人账号", required = true),
            @ApiImplicitParam(name = "title", value = "标题"),
    })
    @PostMapping("/to_mail_read")
    @ResponseBody
    public AjaxResult toRead(String toSend, String title) {
        WebMail mail = new WebMail();
        mail.setToSend(toSend);
        mail.setTitle(title);
        startPage();
        //查看指定账号的全部已读邮件
        List<WebMail> l = webMailService.selectMailRead(mail);
        for (WebMail webmail : l) {
            String sendUser1 = webmail.getSendUser();
            if (sendUser1.length() > 14) {
                webmail.setSendUser(sendUser1.substring(0, sendUser1.length() - 14));
            }
        }
        TableDataInfo td = getDataTable(l);
        AjaxResult ar = new AjaxResult(AjaxResult.Type.SUCCESS, null);
        ar.put("rows", td.getRows());
        ar.put("total", td.getTotal());
        return ar;
    }

    @fyh
    @ApiOperation(value = "查询有多少未读邮件", notes = "查询有多少未读邮件")
    @ApiResponses({@ApiResponse(code = 200, message = "成功"), @ApiResponse(code = 500, message = "error")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "author", value = "权限码", required = true),
            @ApiImplicitParam(name = "username", value = "账号", required = true),
    })
    @PostMapping("/getNotReadCount")
    @ResponseBody
    public AjaxResult getNotReadCount(String username) {
        WebMail mail = new WebMail();
        mail.setToSend(username);
        List<WebMail> l = webMailService.selectMailUnread(mail);
        AjaxResult ar = new AjaxResult(AjaxResult.Type.SUCCESS, null);
        ar.put("count", l.size());
        return ar;
    }


    @fyh
    @ApiOperation(value = "收件人删除多条邮件", notes = "收件人删除多条邮件")
    @ApiResponses({@ApiResponse(code = 200, message = "成功"), @ApiResponse(code = 500, message = "error")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "author", value = "权限码", required = true),
            @ApiImplicitParam(name = "uuid", value = "示例: uuid1,uuid2,uuid3"),
            @ApiImplicitParam(name = "toSend", value = "收信人账号")
    })
    @PostMapping("/dels_mail")
    @ResponseBody
    public AjaxResult shoudels(String uuid, String toSend) {
        if (StringUtils.isNull(uuid) || StringUtils.isNull(toSend)) {
            return error("不能存在空值");
        }
        return AjaxResult.success(webMailService.deleteByIds(uuid, toSend));
    }


    @fyh
    @ApiOperation(value = "发件人删除多条邮件", notes = "发件人删除多条邮件")
    @ApiResponses({@ApiResponse(code = 200, message = "成功"), @ApiResponse(code = 500, message = "error")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "author", value = "权限码", required = true),
            @ApiImplicitParam(name = "uuid", value = "示例: uuid1,uuid2,uuid3"),
            @ApiImplicitParam(name = "SendUserAccount", value = "发信人账号")
    })
    @PostMapping("/dels_faMail")
    @ResponseBody
    public AjaxResult fadels(String uuid, String SendUserAccount) {
        if (StringUtils.isNull(uuid) || StringUtils.isNull(SendUserAccount)) {
            return error("不能存在空值");
        }
        return AjaxResult.success(webMailService.deleteFaByIds(uuid, SendUserAccount));
    }
}
